<?php

if (!isset($_SESSION)) {
//On ouvre la session
    session_start();

//Par defaut, on indique que le contenu est du HTML avec un encodage UTF-8
    header('Content-Type: text/html; charset=utf-8');

//Par defaut, la locale est francaise pour le temps et ou l'heure
    setlocale(LC_TIME, 'fr_FR.utf8', 'fra');

//on active la compression gzib ssi elle est autorisee
    if (in_array('ob_gzhandler', ob_list_handlers())) {
        ob_start('ob_gzhandler');
    }
}

//fonction appelee automatiquement lorsque l'on instancie un objet
function __autoload($classname) {
    global $extPaths;
	$found = false;
    $paths = array(
        '',
        'database/',
        'dom/',
        'mail/',
        'app/',
        'form/',
        'dom/',
        'webservice/',
        'crawler/'
    );
    foreach ($paths as $path) {
        $fullpath = ENGINE_ROOT . '/classes/' . $path . $classname . '.php';
//        echo '<pre>$fullpath::',$fullpath,'</pre>';
        if (file_exists($fullpath)) {
            include_once $fullpath;
			$found = true;
            break;
        }
    }
    //surcharge
    if (is_array($extPaths) && count($extPaths) && !$found) {
        foreach ($extPaths as $path) {
            if (!is_dir($path)) {
                $path = DOCUMENT_ROOT . $path;
            }
            $fullpath = $path . $classname . '.php';
            if (file_exists($fullpath)) {
                include_once $fullpath;
				$found = true;
                break;
            }
        }
    }
	if(!$found){
		exit("Class '$classname' not found ! check camelcase or directory");
	}
}

function callstack() {
    $retval = "<pre>callstack:\n";
    $backtrace = debug_backtrace();
    for ($idx = count($backtrace) - 1; $idx > 0; $idx--) {
        $item = $backtrace[$idx];
        if (isset($item['file']))
            $file = sprintf('%s[%03d]', pathinfo($item['file'], PATHINFO_FILENAME), $item['line']);
        else
            $file = 'eval()';
        if (isset($item['class']))
            $func = $item['class'] . '->' . $item['function'];
        else
            $func = $item['function'];
        $retval .= sprintf("\t%-32s\t%s()\n", $file, $func);
    }
    $retval = "</pre>";
    return $retval;
}

//On definit ou se trouve le moteur
define('ENGINE_ROOT', dirname(__FILE__));

/**
 * A garder en attendant de tester sur le serveur OVH
 * */
$docRoot = $_SERVER['DOCUMENT_ROOT'];

if (!empty($docRoot)) {
    define('DOCUMENT_ROOT', substr($docRoot, -1) == '/' ? substr($docRoot, 0, -1) : $docRoot);
} elseif (!empty($_SERVER['DOCUMENT_ROOT'])) {
    define('DOCUMENT_ROOT', substr($_SERVER['DOCUMENT_ROOT'], -1) == '/' ? substr($_SERVER['DOCUMENT_ROOT'], 0, -1) : $_SERVER['DOCUMENT_ROOT'] );
} else {
    define('DOCUMENT_ROOT', '/var/www');
}
unset($docRoot);

// on definit le temps de debut du script
define('START_TIME', Page::startTime());

//Gestion du debug
if (!defined('DEBUG')) {
    $_engine_debug = (isset($_SERVER['SERVER_ADDR']) && in_array($_SERVER['SERVER_ADDR'], array('192.168.0.20', '192.168.25.244', '88.191.116.103')));
    $_engine_debug = isset($_GET['_engine_debug']) ? filter_var($_GET['_engine_debug'], FILTER_VALIDATE_BOOLEAN) : $_engine_debug;
    define('DEBUG', $_engine_debug);
}

//Gestion des erreur selon le mode DEBUG
if (DEBUG) {
    error_reporting(E_ALL);
} else {
    error_reporting(0);
}

define('CACHE_PATH', DOCUMENT_ROOT . '/_cache/');

//if(!is_dir(CACHE_PATH)){
//    throw new Exception("Watch out ! Cache directory ".CACHE_PATH." is not available");
//}
?>